<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <!-- <div class="sss frames"></div> -->
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <!--     { min: 0, max: 0, txt: "我还可以兑换" }, -->
          <span
            :class="{ active: options1Index == index }"
            :key="index"
            v-for="(item, index) in options1"
            @click="options1Fn(index, item.min, item.max)"
          >
            {{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            v-for="(item, index) in options2"
            :key="index"
            :class="{ active: index == num2 }"
            @click="
              num2 = index;
              GoodsSearchParams.type = item.type;
            "
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div v-show="!isReachBottom">
        <i class="iconfont icon-loading frames"></i>loading...
      </div>
      <div v-show="isReachBottom">加载完毕</div>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumv.vue";
import List from "../components/home/List.vue";
import { GoodsSearchAPI } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/util";
export default {
  beforeRouteUpdate(to, from, next) {
    console.log("触发了");
    // 在当前路由改变，但是该组件被复用时调用
    // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，
    // 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
    // 可以访问组件实例 `this`
  },
  data() {
    return {
      goodsList: [],
      goodsListShow: [],
      GoodsSearchParams: {
        did: 0,
        type: 0,
        min: 0,
        max: 100000,
        keyword: "",
      },
      // 分类数组项
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      // 排序当前项
      num1: 0,
      // 分类当前项
      num2: 0,

      options1Index: 0,
      // 排序的数组项
      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      page: 1,
      size: 8,
      isLoading: true,
      isReachBottom: false,
    };
  },
  components: {
    Crumb,
    List,
  },
  async created() {
    console.log("在created获取数据", this.$route.query.keyword);
    this.GoodsSearchParams.keyword = this.$route.query.keyword || "";
    this.GoodsSearch();
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    scrollFn() {
      // console.log("滚动事件");
      //getScrollTop,getClientHeight,getScrollHeight
      //getScrollTop滚动距离
      //getClientHeight用户可视区域
      //getScrollHeight整个文档高度
      // console.log(document.documentElement.scrollWidth);
      if (this.isLoading) {
        if (getScrollHeight() - 240 < getScrollTop() + getClientHeight()) {
          this.isLoading = false;
          console.log("整个文档高度-240<滚动距离+用户可视区域");
          this.page++;
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
            }
            if (this.goodsListShow.length == this.goodsList.length) {
              this.isReachBottom = true;
              console.log("到底部了");
            } else {
              this.isLoading = true;
            }
          }, 5000);
        }
      }
    },
    options1Fn(index, min, max) {
      this.GoodsSearchParams.min = min;
      this.GoodsSearchParams.max = max;
      this.options1Index = index;
    },
    async GoodsSearch() {
      let res = await GoodsSearchAPI({
        did: this.GoodsSearchParams.did,
        type: this.GoodsSearchParams.type,
        min: this.GoodsSearchParams.min,
        max: this.GoodsSearchParams.max,
        keyword: this.GoodsSearchParams.keyword,
      });
      this.goodsList = res.data;
      //获取前8条数据
      this.goodsListShow = this.goodsList.filter(
        (item, index) => index < this.size
      );
      //数据初始化
      this.isLoading = true;
      this.isReachBottom = false;
      this.page = 1;
      if (this.goodsListShow.length == this.goodsList.length) {
        this.isReachBottom = true;
        console.log("到底部了");
      } else {
        this.isLoading = true;
      }
    },
  },
  watch: {
    GoodsSearchParams: {
      deep: true,
      handler() {
        this.GoodsSearch();
      },
      immediate: true,
    },
    "$route.query.keyword": {
      handler(nweVal) {
        this.GoodsSearchParams.keyword = nweVal || "";
      },
      immediate: true,
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";
@keyframes aa {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
.frames {
  display: inline-block;
  animation: aa 1s infinite linear;
}
.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
.sss {
  width: 100px;
  height: 100px;
  background-color: pink;
}
</style>